package com.interview.javabase.algorithm.动态规划;

/**
 * @author www.byteblogs.com
 * @date 2021-10-12
 * @since 2.0
 */
public class 以最小的插入次数构造回文串 {

    public static void main(String[] args) {
        System.out.println("mixInsertions() = " + mixInsertions("aabba"));
    }

    public static int mixInsertions(String s) {
        int n = s.length();
        char[] c = s.toCharArray();

        int[][] dp = new int[n][n];

        for (int i = n - 2; i >= 0; i--) {
            for (int j = i + 1; j < n; j++) {
                if (c[i] == c[j]) {
                    dp[i][j] = dp[i + 1][j - 1];
                } else {
                    dp[i][j] = Math.min(dp[i][j - 1], dp[i + 1][j]) + 1;
                }
            }
        }

        return dp[0][n - 1];
    }

}
